package com.fulihui.wgj.admin.api.base;

import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.upload.OSSUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import static com.fulihui.common.error.Errors.Commons.UPLOAD_FAIL;

/**
 * @author: Levon
 * @date: 2018-08-06 11:18
 */
@RestController
@RequestMapping("/oss")
@Api(tags = "阿里云oss存储相关接口")
public class OssController {

    @Autowired
    private OSSUpload ossUpload;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiOperation("上传文件")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile multfile) {

        String fileUrl = ossUpload.upload(multfile);
        if (StringUtils.isEmpty(fileUrl)) {
            return JsonResultBuilder.fail(UPLOAD_FAIL.getErrcode(), UPLOAD_FAIL.getErrmsg());
        }
        return JsonResultBuilder.succ(fileUrl);
    }
}
